package examples;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.hyperrealm.kiwi.ui.*;
import com.hyperrealm.kiwi.ui.dialog.*;
import com.hyperrealm.kiwi.util.*;

public class WizardExample
{
  private Config config;
  
  public static void main(String args[])
  {
    WizardExample w = new WizardExample();
  }

  WizardExample()
  {
    Frame pf = KiwiUtils.getPhantomFrame();

    config = new Config();
    
    WizardPanelSequence seq = new WizardPanelSequence(config);


    // panel #1

    seq.addPanel(new WizardPanel()
      {

        {
          setTitle("Introduction");
        }

        protected Component buildUI()
        {
          KLabelArea la = new KLabelArea(5, 20);
          la.setText("You can put anything you want in a WizardPanel. In this case, we just have a label area containing some text for the user to read.");

          return(la);
        }

        public void syncData () { }
        public void syncUI() { }
        
      });

    // panel #2
    
    seq.addPanel(new WizardPanel()
      {
        private JTextField t_key;

        {
          setTitle("License Key Entry");
        }
        
        protected Component buildUI()
        {
          KPanel p = new KPanel();
          p.setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();

          gbc.anchor = gbc.WEST;
          gbc.fill = gbc.HORIZONTAL;
          gbc.weightx = 1;

          gbc.gridwidth = gbc.REMAINDER;;
          KLabelArea la = new KLabelArea("Please enter your license key. This is the key that you received by email when you registered your product.", 3, 20);
          gbc.insets = KiwiUtils.lastInsets;
          p.add(la, gbc);

          gbc.insets = KiwiUtils.firstBottomInsets;
          gbc.gridwidth = 1;
          gbc.weightx = 0;
          p.add(new KLabel("License Key:"), gbc);

          gbc.weightx = 1;
          t_key = new JTextField(20);

          // We fire a change event every time the text in the text field
          // changes. This causes the WizardView to requery the WizardPanel
          // regarding whether movement to the next panel is allowed. See
          // the canMoveForward() method below.

          t_key.getDocument().addDocumentListener(new DocumentListener()
            {
              public void changedUpdate(DocumentEvent evt)
              {
                fireChangeEvent();
              }

              public void insertUpdate(DocumentEvent evt)
              {
                fireChangeEvent();
              }

              public void removeUpdate(DocumentEvent evt)
              {
                fireChangeEvent();
              }
              
            });
          
          gbc.gridwidth = gbc.REMAINDER;;
          gbc.insets = KiwiUtils.lastInsets;
          p.add(t_key, gbc);


          KLabel l = new KLabel();
          gbc.weighty = 1;
          gbc.fill = gbc.BOTH;
          p.add(l, gbc);

          return(p);
        }

        public void syncData()
        {
          config.putString("key", t_key.getText());
        }

        public void syncUI()
        {
          t_key.setText(config.getString("key", ""));
        }

        public void beginFocus()
        {
          t_key.requestFocus();
        }

        // Only allow movement to the next panel if something has been
        // entered in the text field.

        public boolean canMoveForward()
        {
          String s = t_key.getText();

          return(s.length() > 0);
        }
        
      });

    // panel #3

    seq.addPanel(new WizardPanel()
      {
        private KCheckBox b_audio;

        {
          setTitle("Audio Options");
        }

        public void beginFocus()
        {
          b_audio.requestFocus();
        }

        protected Component buildUI()
        {
          KPanel p = new KPanel();
          p.setLayout(new BorderLayout(5, 5));

          KLabelArea la = new KLabelArea(5, 20);
          la.setText("And finally here's another example panel where you can enable a hypothetical option.");
          
          p.add("North", la);
          
          b_audio = new KCheckBox("Play sounds");
          p.add("South", b_audio);

          return(p);
        }

        public void syncData()
        {
          config.putBoolean("sounds", b_audio.isSelected());
        }

        public void syncUI()
        {
          b_audio.setSelected(config.getBoolean("sounds"));
        }
        
      });
    
    
    WizardView wizv = new WizardView(seq);

    
    
    WizardDialog wizd = new WizardDialog(pf, "WizardDialog Example", true,
                                         wizv);

    ResourceLoader loader = new ResourceLoader(getClass());
    
    wizv.setIcon(KiwiUtils.getResourceManager().getIcon("wizard.gif"));

    wizd.setSize(new Dimension(500, 300));

    wizd.setVisible(true);

    if(wizd.isCancelled())
      System.out.println("Wizard cancelled.");
    else
    {
      System.out.println("Wizard completed.");

      System.out.println("key: " + config.getString("key"));
      System.out.println("sounds: " + config.getBoolean("sounds"));
    }
    
  }
}
